package com.smile.weather.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.smile.weather.R;
import com.smile.weather.fragment.BaseFragment;
import com.smile.weather.fragment.SevenDayFragment;
import com.smile.weather.fragment.TodayFragment;
import com.smile.weather.interfaces.IMainView;
import com.smile.weather.presenter.MainViewPresenter;
import com.smile.weather.swiperefresh.PullToRefreshBase;
import com.smile.weather.swiperefresh.PullToRefreshScrollView;

public class WeatherActivity extends BaseActivity implements View.OnClickListener, IMainView {

    private PullToRefreshScrollView mPullToRefreshScrollView;
    private ScrollView mScrollView;

    //选择城市
    private RelativeLayout rl_city;
    //城市名称
    private TextView tv_city;
    //最新更新的时间
    private TextView tv_time;
    //导航栏(今天)
    private TextView tv_today;
    //导航栏(7天)
    private TextView tv_7day;

    private FragmentManager fm;
    private FragmentTransaction ft;

    // 主页的两个页面
    private BaseFragment[] mFragment;
    // 当前显示的页面
    private BaseFragment nowFragment;
    // 记录点击过的按钮
    private int preSelect = 0;
    // 城市id
    private String cityId = "101281502";

    private MainViewPresenter mainViewPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather);

        initWidget();

    }

    /**
     * 对控件初始化
     */
    private void initWidget() {

        mainViewPresenter = new MainViewPresenter(this);
        mainViewPresenter.setiMainView(this);

        mPullToRefreshScrollView = (PullToRefreshScrollView) findViewById(R.id.pull_refresh_scrollview);
        mPullToRefreshScrollView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener<ScrollView>() {
            @Override
            public void onRefresh(PullToRefreshBase<ScrollView> refreshView) {
                mainViewPresenter.getCityWeather(cityId);
            }
        });
        mScrollView = mPullToRefreshScrollView.getRefreshableView();

        rl_city = (RelativeLayout) findViewById(R.id.rl_city);
        tv_city = (TextView) findViewById(R.id.tv_city);
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_today = (TextView) findViewById(R.id.tv_today);
        tv_7day = (TextView) findViewById(R.id.tv_7day);

        rl_city.setOnClickListener(this);
        tv_today.setOnClickListener(this);
        tv_7day.setOnClickListener(this);

        mFragment = new BaseFragment[2];
        mFragment[0] = new TodayFragment();
        mFragment[1] = new SevenDayFragment();

        fm = getSupportFragmentManager();
        ft = fm.beginTransaction();
        ft.add(R.id.ll_main_view, mFragment[0]);
        ft.add(R.id.ll_main_view, mFragment[1]);
        ft.hide(mFragment[1]);
        ft.commit();
        nowFragment = mFragment[0];

        tv_city.setText("饶平");

        mainViewPresenter.setmFragment(mFragment);
        mainViewPresenter.getCityWeather(cityId);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_city:
                Toast.makeText(getApplicationContext(), "选择城市", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tv_today:
                setView(0);
                break;
            case R.id.tv_7day:
                setView(1);
                break;
            default:
                break;
        }
    }

    private void setView(int n) {
        // 连续点击同个导航则不错处理
        if (preSelect != n) {
            ft = fm.beginTransaction();
            ft.hide(nowFragment);
            nowFragment = mFragment[n];
            ft.show(nowFragment);
            ft.commit();
            preSelect = n;

            switch (n) {
                case 0:
                    tv_today.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color
                            .tv_dark_bg_color));
                    tv_today.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color
                            .white));
                    tv_7day.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color
                            .tv_light_bg_color));
                    tv_7day.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color
                            .text_color));
                    break;
                case 1:
                    tv_7day.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color
                            .tv_dark_bg_color));
                    tv_7day.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color
                            .white));
                    tv_today.setBackgroundColor(ContextCompat.getColor(getApplicationContext(), R.color
                            .tv_light_bg_color));
                    tv_today.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color
                            .text_color));
                    break;
            }
        }
    }

    @Override
    public void setTime(String time) {
        tv_time.setText(time);
    }

    @Override
    public void refreshComplete() {
        mPullToRefreshScrollView.onRefreshComplete();
    }
}
